import { createRouter, createWebHistory } from 'vue-router'
import cookie from 'js-cookie'

const NotFound = () => import('@/views/404.vue')
const Index = () => import('@/views/Index.vue')
const Login = () => import('@/views/Login.vue')

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'indexView',
      component: Index,
      redirect: '/index/dashboard',
      children: [
        {
          path: 'redirectPage/:pathMatch(.*)*',
          name: 'redirectPage',
          component: () => import('../views/RedirectPage.vue'),
          meta: {
            title: ''
          }
        },
        {
          path:'userInfo',
          name:'userInfo',
          component:()=>import('../views/person/UserInfo.vue'),
          meta: {
            title: '个人信息'
          }
        },{
          path:'userPwd',
          name:'userPwd',
          component:()=>import('../views/person/UserPwd.vue'),
          meta:{
            title:"修改密码"
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/notfound',
      component: NotFound
    }
  ]
})
//路由守卫
router.beforeEach((to, from) => {
  if ('login' !== to.name && !cookie.get('adminToken')) {
    return {path:'/login',query:{back:to.fullPath}}
  }
  return true
})
export default router
